<template>
  <div>
    <svg-icon class-name="cloudDownload" icon-class="cloudDownload" @click.stop="click"/>

    <!-- 下载中心弹出框-->
    <el-dialog title="下载中心" :visible.sync="dialogVisible" width="70%" append-to-body>
      <div :style="'height: calc(70vh - 120px)'">
        <el-alert
          title="下载中心说明"
          type="warning"
          description="文件只保留7天，请及时下载"
          show-icon
          :closable="false">
        </el-alert>
        <el-table v-loading="loading" :data="downloadCenterList" :header-cell-style="{'text-align':'center'}" height="85%" :row-class-name="tableRowClassName">
          <el-table-column label="序号" align="center" width="50">
            <template slot-scope="scope">
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="文件名称" align="left" prop="fileName" show-overflow-tooltip>
            <template v-slot="scope">
              <svg-icon :class-name="getFileTypeName(scope.row.fileType)" :icon-class="getFileTypeName(scope.row.fileType)" /> {{scope.row.fileName}}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="140">
            <template v-slot="scope">
              <span>{{ $parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="下载时间" align="center" prop="downloadTime" width="140">
            <template v-slot="scope">
              <span>{{ $parseTime(scope.row.downloadTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="耗时" align="center" width="100" show-overflow-tooltip>
            <template v-slot="scope">
              <span>{{ $diffTime(scope.row.buildStartTime, scope.row.buildEndTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="文件大小" align="center" prop="fileSize" width="80" show-overflow-tooltip>
            <template v-slot="scope">
              <span>{{ $fileSize(scope.row.fileSize) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="MD5" align="center" prop="fileMd5" width="300"/>
          <el-table-column label="状态" align="center" prop="fileStatus" width="80">
            <template v-slot="scope">
              <dict-tag :options="dict.type.sys_download_center" :value="scope.row.fileStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="userId" width="100">
            <template v-slot="scope">
              <user-tag :value="scope.row.userId" type="userId"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template v-slot="scope">
              <el-button
                type="text"
                v-if="scope.row.fileStatus === '1' || scope.row.fileStatus === '2'"
                @click="handleDownload(scope.row)" >
                <svg-icon class-name="download" icon-class="download"/>
                {{ scope.row.fileStatus === '1' ? "下载" : "重新下载" }}</el-button>
              <el-button
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['tool:downloadCenter:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          style="bottom: 10px"
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDownloadCenter, delDownloadCenter} from "@/api/tool/downloadCenter";
import {fileSize} from "@/utils/basic";

export default {
  name: "CloudDownload",
  dicts: ['sys_download_center'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出框状态
      dialogVisible: false,
      // 总条数
      total: 0,
      // 下载中心表格数据
      downloadCenterList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  methods: {
    fileSize,
    /** 查询下载中心列表 */
    getList() {
      this.loading = true;
      listDownloadCenter(this.queryParams).then(response => {
        this.downloadCenterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    click(){
      this.getList();
      this.dialogVisible = true;
    },
    /** 返回文件类型 */
    getFileTypeName(number) {
      const fileTypeMap = {
        '1': 'excel',
        '2': 'word',
        '3': 'ppt',
        '4': 'pdf',
        '5': 'txt'
      };
      return fileTypeMap[number] || 'file';
    },
    /** 表格背景色 */
    tableRowClassName({ row, rowIndex }){
      if(row.fileStatus === '-1'){
        return 'warning-row';
      }
      return '';
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除【'+row.fileName+'】的数据项？').then(function() {
        return delDownloadCenter(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** socket信息返回接受函数 */
    getSocketResult(data) {
      if(data.detail.info.code === 200){
        this.getList();
        this.$modal.msgSuccess(data.detail.info.msg);
      } else if (data.detail.info.code !== 1000 && data.detail.info.code !== 200){
        this.$modal.msgError(data.detail.info.msg);
      }
    },
    /** 下载按钮操作 */
    async handleDownload(row){
      await this.$download.resource(row.id);
      setTimeout(() => {
        this.getList();
      }, 1000); // 延迟 1 秒

    },
  },
  mounted() {
    /** websocket接收数据 */
    window.addEventListener('onmessageWS', this.getSocketResult)
  },
}
</script>

<style lang="scss" scoped>
.cloudDownload {
  cursor: pointer;
}
/* 文件生成失败背景色 */
::v-deep .el-table .warning-row td{
  background: #F56C6C!important;
}
</style>
